home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlutil.zip / FILEMODE.C < prev    next >
C/C++ Source or Header  |  1990-05-31  |  5KB  |  197 lines

  1. /* filemode.c -- make a string describing file modes
  2.    Copyright (C) 1985, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #ifdef _POSIX_SOURCE
  21. #define S_IREAD S_IRUSR
  22. #define S_IWRITE S_IWUSR
  23. #define S_IEXEC S_IXUSR
  24. #endif
  25.  
  26. void mode_string ();
  27. static char ftypelet ();
  28. static void rwx ();
  29. static void setst ();
  30.  
  31. /* filemodestring - fill in string STR with an ls-style ASCII
  32.    representation of the st_mode field of file stats block STATP.
  33.    10 characters are stored in STR; no terminating null is added.
  34.    The characters stored in STR are:
  35.  
  36.    0    File type.  'd' for directory, 'c' for character
  37.     special, 'b' for block special, 'm' for multiplex,
  38.     'l' for symbolic link, 's' for socket, 'p' for fifo,
  39.     '-' for any other file type
  40.  
  41.    1    'r' if the owner may read, '-' otherwise.
  42.  
  43.    2    'w' if the owner may write, '-' otherwise.
  44.  
  45.    3    'x' if the owner may execute, 's' if the file is
  46.     set-user-id, '-' otherwise.
  47.     'S' if the file is set-user-id, but the execute
  48.     bit isn't set.
  49.  
  50.    4    'r' if group members may read, '-' otherwise.
  51.  
  52.    5    'w' if group members may write, '-' otherwise.
  53.  
  54.    6    'x' if group members may execute, 's' if the file is
  55.     set-group-id, '-' otherwise.
  56.     'S' if it is set-group-id but not executable.
  57.  
  58.    7    'r' if any user may read, '-' otherwise.
  59.  
  60.    8    'w' if any user may write, '-' otherwise.
  61.  
  62.    9    'x' if any user may execute, 't' if the file is "sticky"
  63.     (will be retained in swap space after execution), '-'
  64.     otherwise.
  65.     'T' if the file is sticky but not executable. */
  66.  
  67. void
  68. filemodestring (statp, str)
  69.      struct stat *statp;
  70.      char *str;
  71. {
  72.   mode_string (statp->st_mode, str);
  73. }
  74.  
  75. /* Like filemodestring, but only the relevant part of the `struct stat'
  76.    is given as an argument. */
  77.  
  78. void
  79. mode_string (mode, str)
  80.      unsigned short mode;
  81.      char *str;
  82. {
  83.   str[0] = ftypelet (mode);
  84.   rwx ((mode & 0700) << 0, &str[1]);
  85.   rwx ((mode & 0070) << 3, &str[4]);
  86.   rwx ((mode & 0007) << 6, &str[7]);
  87.   setst (mode, str);
  88. }
  89.  
  90. /* Return a character indicating the type of file described by
  91.    file mode BITS:
  92.    'd' for directories
  93.    'b' for block special files
  94.    'c' for character special files
  95.    'm' for multiplexor files
  96.    'l' for symbolic links
  97.    's' for sockets
  98.    'p' for fifos
  99.    '-' for any other file type. */
  100.  
  101. static char
  102. ftypelet (bits)
  103.      unsigned short bits;
  104. {
  105.   switch (bits & S_IFMT)
  106.     {
  107.     default:
  108.       return '-';
  109.     case S_IFDIR:
  110.       return 'd';
  111. #ifdef S_IFLNK
  112.     case S_IFLNK:
  113.       return 'l';
  114. #endif
  115. #ifdef S_IFCHR
  116.     case S_IFCHR:
  117.       return 'c';
  118. #endif
  119. #ifdef S_IFBLK
  120.     case S_IFBLK:
  121.       return 'b';
  122. #endif
  123. #ifdef S_IFMPC
  124.     case S_IFMPC:
  125.     case S_IFMPB:
  126.       return 'm';
  127. #endif
  128. #ifdef S_IFSOCK
  129.     case S_IFSOCK:
  130.       return 's';
  131. #endif
  132. #ifdef S_IFIFO
  133. #if S_IFIFO != S_IFSOCK
  134.     case S_IFIFO:
  135.       return 'p';
  136. #endif
  137. #endif
  138. #ifdef S_IFNWK            /* HP-UX */
  139.     case S_IFNWK:
  140.       return 'n';
  141. #endif
  142.     }
  143. }
  144.  
  145. /* Look at read, write, and execute bits in BITS and set
  146.    flags in CHARS accordingly. */
  147.  
  148. static void
  149. rwx (bits, chars)
  150.      unsigned short bits;
  151.      char *chars;
  152. {
  153.   chars[0] = (bits & S_IREAD) ? 'r' : '-';
  154.   chars[1] = (bits & S_IWRITE) ? 'w' : '-';
  155.   chars[2] = (bits & S_IEXEC) ? 'x' : '-';
  156. }
  157.  
  158. /* Set the 's' and 't' flags in file attributes string CHARS,
  159.    according to the file mode BITS. */
  160.  
  161. static void
  162. setst (bits, chars)
  163.      unsigned short bits;
  164.      char *chars;
  165. {
  166. #ifdef S_ISUID
  167.   if (bits & S_ISUID)
  168.     {
  169.       if (chars[3] != 'x')
  170.     /* Set-uid, but not executable by owner. */
  171.     chars[3] = 'S';
  172.       else
  173.     chars[3] = 's';
  174.     }
  175. #endif
  176. #ifdef S_ISGID
  177.   if (bits & S_ISGID)
  178.     {
  179.       if (chars[6] != 'x')
  180.     /* Set-gid, but not executable by group. */
  181.     chars[6] = 'S';
  182.       else
  183.     chars[6] = 's';
  184.     }
  185. #endif
  186. #ifdef S_ISVTX
  187.   if (bits & S_ISVTX)
  188.     {
  189.       if (chars[9] != 'x')
  190.     /* Sticky, but not executable by others. */
  191.     chars[9] = 'T';
  192.       else
  193.     chars[9] = 't';
  194.     }
  195. #endif
  196. }
  197.